package com.example.shellcollector.util;

import android.content.Context;
import android.content.SharedPreferences;

import androidx.preference.PreferenceManager;

/**
 * Custom preference class designed for Shell Collector!
 */
public final class ShellPreference {
    private long startTime;
    private long endTime;
    private long frequency;

    public long getStartTime() {
        return startTime;
    }

    public long getEndTime() {
        return endTime;
    }

    public long getFrequency() {
        return frequency;
    }

    public void setStartTime(long startTime) {
        this.startTime = startTime;
    }

    public void setEndTime(long endTime) {
        this.endTime = endTime;
    }

    public void setFrequency(long frequency) {
        this.frequency = frequency;
    }

    public static ShellPreference getPreference(Context context) {
        long start;
        long end;

        SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
        long startTime = preferences.getLong("start", 0);
        long endTime = preferences.getLong("end", 0);
        if (startTime == 0 || endTime == 0) {
            start = AppUsageCollectHelper.getPreviousDaysTimestamp(7);
            end = System.currentTimeMillis();
        } else {
            start = startTime;
            end = endTime;
        }

        ShellPreference preference = new ShellPreference();
        preference.setStartTime(start);
        preference.setEndTime(end);

        return preference;
    }
}
